# coding=utf-8
from django.db import models
import sys
import json

reload(sys)
sys.setdefaultencoding('utf-8')

class SimpleBaseMode(models.Model):
    is_valid = models.BooleanField(default=True, verbose_name=u'有效性')
    is_deleted = models.BooleanField(default=False, verbose_name=u'是否被删除')

    class Meta:
        abstract = True
        db_table = 'simple_base_mode'
        app_label = 'spider'  # 定义该model的app_label

    def valid_action(self, valid=True):
        self.is_value = valid
        self.save()


class BaseModel(SimpleBaseMode):
    created_time = models.DateTimeField(auto_now_add=True, verbose_name=u'创建时间')
    modified_time = models.DateTimeField(auto_now=True, verbose_name=u'修改时间')

    class Meta:
        abstract = True
        db_table = 'base_model'
        app_label = 'spider'


class Shop(BaseModel):
    name = models.CharField(u'商店名称', max_length=50)
    url = models.CharField(u'商店网址', max_length=100)

    # def to_json_string(self):
    #     return json.dumps(self.__to_dict__(), ensure_ascii=False).decode('utf-8').encode('gb2312')

    class Meta:
        db_table = 'shop'
        app_label = 'spider'

    def to_dict(self):
        return {
            'shop_id': self.id,
            'shop_name': self.name,
            'shop_url': self.url,
        }

    def __unicode__(self):
        return 'shopId=' + str(self.id) + ' name=' + self.name


# 产品类
class Product(BaseModel):
    name = models.CharField(u'产品名称', max_length=50)
    displayed_name = models.CharField(u'展示的产品名称', max_length=50, default='')
    url = models.CharField(u'产品网站', max_length=100)
    price = models.FloatField(u'商品价格')
    displayed_price = models.FloatField(u'商品价格', null=True)
    count = models.IntegerField(u'商品总库存', default=0)
    image = models.CharField(u'商品在列表中的图片_虽然重复了_在爬取时有用', max_length=100)
    is_crawled_detail = models.BooleanField(u'是否爬取商品细节', default=False)
    is_publish = models.BooleanField(u'是否发布', default=False)

    shop = models.ForeignKey(Shop)

    class Meta:
        db_table = 'product'
        app_label = 'spider'


    def __unicode__(self):
        return 'id=' + str(self.id).decode('utf-8') + ' name=' + self.name.decode('utf-8')

# 尺寸类
class Size(SimpleBaseMode):
    name = models.CharField(u'尺寸名称', max_length=50)
    another_name = models.CharField(u'尺寸别名', max_length=50)
    product = models.ForeignKey(Product)

    class Meta:
        db_table = 'size'
        app_label = 'spider'

    def __unicode__(self):
        return 'id=' + str(self.id) + ' name=' + self.name


# 颜色类
class Color(SimpleBaseMode):
    name = models.CharField(u'颜色名称', max_length=50)
    another_name = models.CharField(u'颜色别名', max_length=50)
    product = models.ForeignKey(Product)

    class Meta:
        db_table = 'color'
        app_label = 'spider'

    def __unicode__(self):
        return 'color_id=' + str(self.id) + ' name=' + self.name

# 库存类
class Inventory(SimpleBaseMode):
    count = models.IntegerField(u'每个sku的库存数量', default=0)
    product = models.ForeignKey(Product)
    size = models.ForeignKey(Size)
    color = models.ForeignKey(Color)

    class Meta:
        db_table = 'inventory'
        app_label = 'spider'

    def __unicode__(self):
        return 'id=' + str(self.id) + ' inventory_count=' + str(self.inventory_count) + ' product_id=' + str(self.product.id)

# # 列表图片类
# class ListImage(SimpleBaseMode):
#     product = models.ForeignKey(Product)
#     local_path = models.CharField(u'图片路径', max_length=50)
#     net_path = models.CharField(u'图片网络路径', max_length=100)
#
#     class Meta:
#         db_table = 'list_image'
#         app_label = 'spider'
#
#     def __unicode__(self):
#         return 'id=' + str(self.id) + ' net_path=' + self.net_path + ' product_id=' + str(
#             self.product.id)

# 顶部图片类
class BoothImage(SimpleBaseMode):
    product = models.ForeignKey(Product)
    local_path = models.CharField(u'图片路径', max_length=50)

    net_path = models.CharField(u'图片网络路径', max_length=100)

    class Meta:
        db_table = 'booth_image'
        app_label = 'spider'

    def __unicode__(self):
        return 'id=' + str(self.id) + ' net_path=' + self.net_path + ' product_id=' + str(
            self.product.id)

# 底部图片类
class DetailImage(SimpleBaseMode):
    product = models.ForeignKey(Product)
    local_path = models.CharField(u'图片路径', max_length=50)
    net_path = models.CharField(u'图片网络路径', max_length=100)

    class Meta:
        db_table = 'detail_image'
        app_label = 'spider'

    def __unicode__(self):
        return 'id=' + str(self.id) + ' net_path=' + self.net_path + ' product=' + str(
            self.product.id)




